package com.bdc.whatwhat.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;
import com.bdc.whatwhat.R;
import com.bdc.whatwhat.Service.WhatWhatService;
import com.bdc.whatwhat.activities.MainActivity;
import com.bdc.whatwhat.authentification.Facebook;
import com.bdc.whatwhat.authentification.GPlus;
import com.bdc.whatwhat.session.SessionManager;
import com.bdc.whatwhat.task.ConnexionTask;
import com.google.android.gms.plus.PlusClient;

/**
 * Created by gfimac on 20/06/2014.
 */
public class ConnexionFragment extends SherlockFragment implements View.OnClickListener, GPlus.GPlusDelegate, Facebook.FacebookDelegate, ConnexionTask.ConnexionTaskDelegate {

    GPlus gplus;
    Facebook facebook;
    View logoRose;
    View view;
    SessionManager sessionManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gplus = new GPlus(getActivity(), this);
        facebook = new Facebook(getActivity(), this);

        sessionManager = new SessionManager(getActivity());
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_connexion, container, false);
        logoRose = view.findViewById(R.id.logoRose);
        ajouterListeners();

        return view;
    }

    private void ajouterListeners() {
        view.findViewById(R.id.gplus_signin).setOnClickListener(this);
        view.findViewById(R.id.facebook_signin).setOnClickListener(this);
    }




    @Override
    public void onResume() {
        super.onResume();
        facebook.onResume();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        facebook.onActivityResult(getActivity(), requestCode, resultCode, data);
        gplus.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onStart() {
        super.onStart();
        gplus.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
        gplus.onStop();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gplus_signin:
                gplus.connect();
                break;
            case R.id.facebook_signin:
                facebook.connect();
                break;
        }
    }


    @Override
    public void onGPlusConnected(PlusClient client, String token) {
        String id = client.getCurrentPerson().getId();
        String email = client.getAccountName();

        String nom = "";
        try {
            nom = client.getCurrentPerson().getName().getFamilyName();
        }catch (Exception e){}

        String prenom = "";
        try {
            prenom = client.getCurrentPerson().getName().getGivenName();
        }catch (Exception e){}

        Log.d("NAME",client.getCurrentPerson().getName().toString());

        connecter(email, id, token, nom, prenom, WhatWhatService.ORIGIN_GOOGLE_PLUS);
    }

    @Override
    public void onFacebookConnected(String email, String id, String session, String nom, String prenom) {
        connecter(email, id, session, nom, prenom, WhatWhatService.ORIGIN_FACEBOOK);
    }

    public void connecter(String email, String id, String session, String nom, String prenom, String origin) {
        new ConnexionTask(this).execute(email, origin, id, session, nom, prenom,"android",sessionManager.getJeton());
        Log.w("CONNECT", origin + " " + email + " " + id + " session:" + session);
    }


    @Override
    public void afficherChargement(boolean afficher) {

    }

    @Override
    public void onConnecte(String token) {
        Log.w("TOKEN", token);
        sessionManager.setToken(token);
        if(getActivity() != null) {
            getActivity().finish();
            Intent intent = new Intent(getActivity(), MainActivity.class);
            getActivity().startActivity(intent);
        }
    }

    @Override
    public void onErreurConnexion() {
        Log.w("TOKEN", "onErreurConnexion");
    }

    @Override
    public void onErreurReseau() {
        Log.w("TOKEN", "onErreurReseau");
    }
}